package com.sanyang.pda.presenter;


import com.google.gson.Gson;
import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.ManualPickingBean;
import com.sanyang.pda.bean.PickingDeliveryBean;
import com.sanyang.pda.bean.request.ConfirmOffShelfRequest;
import com.sanyang.pda.bean.request.EnterToBePickedRequest;
import com.sanyang.pda.contract.PickingDeliveryContract;
import com.sanyang.pda.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class PickingDeliveryPresenter extends BasePresenter<PickingDeliveryContract.View> implements PickingDeliveryContract.Presenter {

    @Override
    public void deliveryInfo(String deliveryNo, String pallet,String storagebinId, String barcode) {
        NetUtils.geApi().deliveryInfo(deliveryNo, pallet,storagebinId,barcode)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<PickingDeliveryBean>>() {
                    @Override
                    public void accept(BaseAppEntity<PickingDeliveryBean> manualPickingBeanBaseAppEntity) throws Exception {
                        getView().setList(manualPickingBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void confirmOffShelf(String deliveryNo, String pallet, String storagebinId,  List<String> barcode) {
        ConfirmOffShelfRequest confirmOffShelfRequest=new ConfirmOffShelfRequest();
        confirmOffShelfRequest.setArticleIds(barcode);
        confirmOffShelfRequest.setDeliveryNo(deliveryNo);
        confirmOffShelfRequest.setPallet(pallet);
        confirmOffShelfRequest.setStoragebinId(storagebinId);
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(confirmOffShelfRequest));

        NetUtils.geApi().confirmOffShelf(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ManualPickingBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ManualPickingBean> manualPickingBeanBaseAppEntity) throws Exception {
                        getView().showMessage(manualPickingBeanBaseAppEntity.getMsg());
                        getView().onSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void enterToBePicked(EnterToBePickedRequest request) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(request));
        NetUtils.geApi().enterToBePicked(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ManualPickingBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ManualPickingBean> manualPickingBeanBaseAppEntity) throws Exception {
                        getView().showMessage(manualPickingBeanBaseAppEntity.getMsg());
                        getView().onSuccess();
                        //getView().refresh();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void pickingCompleted(EnterToBePickedRequest request) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(request));
        NetUtils.geApi().pickingCompleted(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ManualPickingBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ManualPickingBean> manualPickingBeanBaseAppEntity) throws Exception {
                        getView().showMessage(manualPickingBeanBaseAppEntity.getMsg());
                        getView().onSuccess();
                        //getView().refresh();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
    @Override
    public void selectArticleInfoByBarcode(String barcode,String number) {
        NetUtils.geApi().selectArticleInfoByBarCodeAndNo(barcode,number)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<ByBarcodeBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<ByBarcodeBean>> listBaseAppEntity) throws Exception {
                        getView().setGoodsList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
